/**
 * 
 */
package br.com.localfloripa.controllers;

import java.util.List;
import java.util.Map;

import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import br.com.localfloripa.entities.Anuncio;
import br.com.localfloripa.services.AnuncioService;

/**
 * @author alan
 * @20/03/2012
 *
 */

@Named("lancamentoController")
@ViewScoped
public class LancamentoController extends MainController {

	private static final long serialVersionUID = -1195787881525493446L;
	private static final Logger log = LoggerFactory.getLogger(LancamentoController.class);	
	
	@Inject
	@Named("anuncioService")
	private AnuncioService anuncioService;
	
	private LazyDataModel<Anuncio> lazyDataModel;
	
	
	public LancamentoController() {
		
	}
	
	public String loadLancamentos() {
		
		try {
			// alterar número de registros por página padrão [ 20 -> 10 ]
			//super.setPageSize(10);
			this.lazyDataModel = new LazyDataModel<Anuncio>() {
	
				@Override
				public List<Anuncio> load(int first, int pageSize, String term,SortOrder order, Map<String, String> params) {
	
					List<Anuncio> dataModel = null;
					try {
						dataModel =  anuncioService.listLancamentosLazy(first, pageSize);								
					} catch (Exception e) {
						// registrar mensagem de erro
						log.error(e.getMessage(),e);						
					}								
					return dataModel;
				}
	
				@Override
				public int getPageSize() {				
					return super.getPageSize();
				}						
			};
			
			this.lazyDataModel.setRowCount(anuncioService.countLancamentos().intValue());			
		} catch (Exception e) {
			// registrar mensagem de erro
			log.error(e.getMessage(),e);
		}	
		return "lancamentos";
	}

	public LazyDataModel<Anuncio> getLazyDataModel() {
		return lazyDataModel;
	}

	public void setLazyDataModel(LazyDataModel<Anuncio> lazyDataModel) {
		this.lazyDataModel = lazyDataModel;
	}
	
	


}
